package com.lh.utils.alTool;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.OSSObject;
import com.lh.utils.AlSet;

import java.io.*;

/**
 * @Author LH
 * @Date 2023/11/3 9:05
 * @PackageName com.lh.utils.alTool
 * @Description: [项目名小写 多个单词用_分隔 | 包名全部小写 | 类 首字母大写 | 变量和方法 首字母小写，多个单词组成时除首字母，其他字母首字母需大写 | 常量 全部大写,多个单词用'_'分割]
 */
//如果查看应用信息,临时文件不存在就会从云端下图片
public class FileDownload {
    public void download(String urlPath,String path) throws com.aliyuncs.exceptions.ClientException {
        // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        // 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String objectName = urlPath;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(AlSet.endpoint, AlSet.accessKeyId,AlSet.accessKeySecret);

        try {
            // 调用ossClient.getObject返回一个OSSObject实例，该实例包含文件内容及文件元信息。
            OSSObject ossObject = ossClient.getObject(AlSet.bucketName, objectName);
            // 调用ossObject.getObjectContent获取文件输入流，可读取此输入流获取其内容。
            InputStream content = ossObject.getObjectContent();
            //转换成本地图片
            if (content != null) {
                    // 创建文件输出流
                    FileOutputStream fileOutputStream = new FileOutputStream(path);
                    // 读取并写入数据到文件输出流中
                    byte[] buffer = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = content.read(buffer)) != -1) {
                        fileOutputStream.write(buffer, 0, bytesRead);
                    }
                    // 关闭文件输出流
                    fileOutputStream.close();
            }
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
